local bozhu = fk.CreateSkill{
  name = "xiaobai__bozhu",
}

local bozhu_spec = {
  anim_type = "control",
  expand_pile = "xiaobai__liuhong_zhuzi",
  derived_piles = "xiaobai__liuhong_zhuzi",
  can_trigger = function (self, event, target, player, data)
    local card = data.card
    return target == player and player:hasSkill(self) and (card:isCommonTrick() or card.type == Card.TypeBasic) and
      (event == fk.TargetConfirmed or data.firstTarget)
  end,
  on_cost = function (self, event, target, player, data)
    local effective = {#player:getCardIds("he")>=3,#player:getPile("xiaobai__liuhong_zhuzi")>=2}
    local room = player.room  ---@type Room
    local all_choices = {"#bozhu_set_card", "#bozhu_get_card", "Cancel"}
    local choices = {"Cancel"}
    if effective[1] then
      table.insert(choices, 1, all_choices[1])
    end
    if effective[2] then
      table.insert(choices, 2, all_choices[2])
    end
    if #choices == 1 then return end
    local choice = room:askToChoice(player, {
      prompt = "#bozhu_choices",
      skill_name = bozhu.name,
      choices = choices,
      all_choices = all_choices
    })
    if choice ~= "Cancel" then
      if choice == all_choices[1] then
        local cards = room:askToCards(player, {
          min_num = 3,
          max_num = 3,
          include_equip = true,
          skill_name = bozhu.name,
          cancelable = true,
          prompt = "#bozhu_choose2set"
        })
        if #cards == 3 then
          event:setCostData(self, {cards = cards, choice = 1})
          return true
        end
      elseif choice == all_choices[2] then
        local cards = room:askToCards(player, {
          max_num = 2,
          min_num = 2,
          include_equip = false,
          skill_name = bozhu.name,
          cancelable = true,
          pattern = ".|.|.|xiaobai__liuhong_zhuzi",
          expand_pile = "xiaobai__liuhong_zhuzi",
          prompt = "#bozhu_choose2get"
        })
        if #cards == 2 then
          event:setCostData(self, {cards = cards, choice = 2})
          return true
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    if cost_data.choice == 1 then
      player:addToPile("xiaobai__liuhong_zhuzi", cost_data.cards, true, bozhu.name)
    elseif cost_data.choice == 2 then
      player.room:obtainCard(player, cost_data.cards, true)
    end
    if player:getHandcardNum() == #player:getPile("xiaobai__liuhong_zhuzi") then
      local choices = {"#bozhu_additionalEffect", "#bozhu_cancel", "Cancel"}
      local choice = room:askToChoice(player, {
        choices = choices,
        skill_name = bozhu.name,
        prompt = "#bozhu_choices"
      })
      if choice == choices[1] then
        data.use.additionalEffect = (data.use.additionalEffect or 0) + 1
      elseif choice == choices[2] then
        data.use.nullifiedTargets = data.use.nullifiedTargets or {}
        table.insertTableIfNeed(data.use.nullifiedTargets, room:getAlivePlayers())
      end
      return true
    end
  end,
}

bozhu:addEffect(fk.TargetConfirmed, bozhu_spec)
bozhu:addEffect(fk.TargetSpecified, bozhu_spec)

Fk:loadTranslationTable{
  ["xiaobai__bozhu"] = "拨珠",
  [":xiaobai__bozhu"] = "当你使用基本牌或普通锦囊牌指定目标或成为基本牌或普通锦囊牌的目标后，你可以选择一项：1、将三张牌置于武将牌上。"..
    "2、获得武将牌上两张牌。然后若你的手牌数与你的武将牌上的牌数相等，你令此牌无效或多结算一次。",
  ["xiaobai__liuhong_zhuzi"] = "算珠",
  ["#bozhu_get_card"] = "获得武将牌上的两张牌",
  ["#bozhu_set_card"] = "将三张牌置于武将牌上",
  ["#bozhu_choose2get"] = "拨珠：你可以获得武将牌上两张牌，然后若武将牌上的牌数与你的手牌数相等，你可以令此牌额外结算一次或无效",
  ["#bozhu_choose2set"] = "拨珠：你可以将三张牌置于武将牌上，然后若武将牌上的牌数与你的手牌数相等，你可以令此牌额外结算一次或无效",
  ["#bozhu_choices"] = "拨珠：选择一项",
  ["#bozhu_additionalEffect"] = "额外结算一次",
  ["#bozhu_cancel"] = "无效",

  ["$xiaobai__bozhu1"] = "掌上运筹，锱铢分处，毫厘不差。",
  ["$xiaobai__bozhu2"] = "框分梁界、珠排星斗，拨划指间留。",
}

return bozhu